package LogUtils

import (
	"github.com/sirupsen/logrus"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

var logger *logrus.Logger
var isInit bool

func LogInfo(args ...interface{}) {
	if !isInit {
		InitLog("./app.log")
		isInit = true
	}
	logger.Info(args)
}

func LogWarn(args ...interface{}) {
	if !isInit {
		InitLog("./app.log")
		isInit = true
	}
	logger.Warn(args)
}

func LogError(args ...interface{}) {
	if !isInit {
		InitLog("./app.log")
		isInit = true
	}
	logger.Error(args)
}
func execPath() (string, error) {
	file, err := exec.LookPath(os.Args[0])
	if err != nil {
		return "", err
	}
	re, err := filepath.Abs(file)
	if err != nil {
		logger.Error("The eacePath failed: %sn", err.Error())
	}
	logger.Infoln("The path is ", re)
	return filepath.Abs(file)
}
func GetCurrentDirectory() string {
	dir, err := filepath.Abs(filepath.Dir(os.Args[0])) //返回绝对路径 filepath.Dir(os.Args[0])去除最后一个元素的路径
	if err != nil {
		log.Fatal(err)
	}
	return strings.Replace(dir, "\\", "/", -1) //将替换成/
}
func InitLog(addr string) {
	// 创建日志记录器
	logger = logrus.New()

	// 设置日志级别为 INFO 级别
	logger.SetLevel(logrus.InfoLevel)

	// 创建控制台日志记录器
	logger.SetFormatter(&logrus.TextFormatter{})

	// 创建文件日志记录器
	_, err := os.OpenFile(addr, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err == nil {
		//logger.SetOutput(io.MultiWriter(file, os.Stdout))
		logger.Info("日志文件地址：", GetCurrentDirectory())
	} else {
		logger.Error("Failed to log to file, using default stderr")
	}
	//defer file.Close()
	//
	//// 记录日志
	//logger.Info("This is an info message")
	//logger.Warn("This is a warning message")
	//logger.Error("This is an error message")

}
